<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Estudiante extends MN_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper(array('url', 'form'));
        $this->load->library('ion_auth');
        $this->load->library('session');
        $this->load->model('Persona_Model');
        $this->load->model('Estudiante_Model');
    }

    public function index() {

        if (!$this->ion_auth->logged_in()) {
            //redirect them to the login page
            redirect('auth/login', 'refresh');
        } elseif (!$this->ion_auth->is_admin()) {
            //redirect them to the home page because they must be an administrator to view this
            $this->_render('home/inicio');
        } else {
            //set the flash data error message if there is one
            $this->_render('home/inicio');
        }
    }

    public function inicio() {
        $this->data['estudiantes'] = $this->Estudiante_Model->getListado();
        $this->_render('estudiantes/start');
    }

    public function crear() {
        $this->load->library('form_validation');
        $this->_render('estudiantes/new');
    }

    public function actualizar() {
        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');

        $opc = $this->uri->segment(3);
        $ide_pers = $this->uri->segment(4);
        if (!isset($_POST['cedula_pers'])) {
            $this->data['estudiante'] = $this->Estudiante_Model->getDatosEstudiante($ide_pers);
            $this->data['opc'] = $opc;
            $this->_render('estudiantes/update');
        } else {
            $ide_pers = $this->input->post('persona_ide_pers');
            $opc = $this->input->post('txtopc');
            $this->data['opc'] = $opc;

            $this->form_validation->set_rules('cedula_pers', 'NUMERO DE CEDULA', 'trim|integer|required');
            $this->form_validation->set_rules('nombre_pers', 'NOMBRES', 'required|trim');
            $this->form_validation->set_rules('apellido_pers', 'APELLIDOS', 'required|trim');
            $this->form_validation->set_rules('nacionalidad_pers', 'NACIONALIDAD', 'required|trim');
            $this->form_validation->set_rules('provincia_pers', 'PROVINCIA', 'required|trim');
            $this->form_validation->set_rules('ciudad_pers', 'CIUDAD', 'required|trim');
            $this->form_validation->set_rules('direccion_pers', 'DIRECCION', 'required|trim');
            $this->form_validation->set_rules('email_pers_est', 'Email personal requerido', 'required|trim|valid_email');
            $this->form_validation->set_rules('planted_procede_est', 'NOMBRE DEL PLANTEL DONDE PROCEDE', 'required|trim');
            $this->form_validation->set_rules('especialidad_est', 'NOMBRE DE LA ESPECIALIDAD', 'required|trim');
            $this->form_validation->set_rules('nombre_madre_est', 'NOMBRE DEL REPRESENTATE', 'required|trim');

            if (($this->form_validation->run() == FALSE)) {
                $this->data['estudiante'] = $this->Estudiante_Model->getDatosEstudiante($ide_pers);

                $this->data['opc'] = $opc;
                echo "error en ingreso de datos";

                //$this->_render('estudiantes/update');
            } else {
                $error_fatal = 0;

                if (!empty($_FILES['foto_pers']['name'])) {
                    $config['upload_path'] = './commons/personas/';
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
                    $config['max_size'] = '3000';
                    $config['max_width'] = '1024';
                    $config['max_height'] = '768';

                    $this->load->library('upload', $config);

                    if (!$this->upload->do_upload('foto_pers')) {
                        $this->data['msj_error'] = $this->upload->display_errors();
                        $upload_data = null;
                        $error_fatal = 0;
                    } else {
                        $upload_data = $this->upload->data();
                        $error_fatal = 1;
                    }
                } else {
                    $error_fatal = 2;
                }
                switch ($error_fatal) {

                    case 0:
                        $this->_render('estudiantes/update');
                        break;
                    case 1:
                    case 2:
                        $persona = array(
                            "cedula_pers" => $this->input->post('cedula_pers'),
                            "tipo_doc_pers" => $this->input->post('tipo_doc_pers'),
                            "nombre_pers" => $this->input->post('nombre_pers'),
                            "apellido_pers" => $this->input->post('apellido_pers'),
                            "nacionalidad_pers" => $this->input->post('nacionalidad_pers'),
                            "provincia_pers" => $this->input->post('provincia_pers'),
                            "ciudad_pers" => $this->input->post('ciudad_pers'),
                            "direccion_pers" => $this->input->post('direccion_pers'),
                            "refer_domic_pers" => $this->input->post('refer_domic_pers'),
                            "tipo_discap_pers" => $this->input->post('tipo_discap_pers'),
                            "conadis_pers" => $this->input->post('conadis_pers'),
                            "porc_disc_pers" => $this->input->post('porc_disc_pers'),
                            "sexo_pers" => $this->input->post('sexo_pers'),
                            "estado_civil_pers" => $this->input->post('estado_civil_pers'),
                            "autodefin_pers" => $this->input->post('autodefin_pers'),
                            "fecha_nac_pers" => date("Y-m-d", strtotime($this->input->post('fecha_nac_pers'))),
                            "fono_pers" => $this->input->post('fono_pers'),
                            "celular_pers" => $this->input->post('celular_pers'),
                            "email_inst_pers" => $this->input->post('email_inst_pers'),
                            "foto_pers" => ($error_fatal == 1 ? base_url() . "commons/personas/" . $_FILES['foto_pers']['name'] : $this->input->post('foto_pers')),
                        );

                        //$persona_ide_pers=1;

                        $estudiante = array(
                            "planted_procede_est" => $this->input->post('planted_procede_est'),
                            "especialidad_est" => $this->input->post('especialidad_est'),
                            "nombre_padre_est" => $this->input->post('nombre_padre_est'),
                            "profesion_padre_est" => $this->input->post('profesion_padre_est'),
                            "nombre_madre_est" => $this->input->post('nombre_madre_est'),
                            "profesion_madre_est" => $this->input->post('profesion_madre_est'),
                            "conjuge_est" => $this->input->post('conjuge_est'),
                            "carga_fam_est" => $this->input->post('carga_fam_est'),
                            "tipo_sangre_est" => $this->input->post('tipo_sangre_est'),
                            "sector_est" => $this->input->post('sector_est'),
                            "email_pers_est" => $this->input->post('email_pers_est'),
                            "face_est" => $this->input->post('face_est'),
                            "twit_est" => $this->input->post('twit_est'),
                            "estud_traba_est" => $this->input->post('estud_traba_est'),
                            "activo_est" => $this->input->post('activo_est'),
                        );


                        if ($opc == 'upd') {
                            $this->Persona_Model->actualizarPersona($persona, $ide_pers);
                            $this->Estudiante_Model->actualizarEstudiante($estudiante, $ide_pers);
                        } else {
                            $this->Estudiante_Model->eliminarEstudiante($ide_pers);
                        }




                        break;
                }
                redirect('estudiante/inicio');
            }
        }
    }

    function presearch() {
        $who = $this->input->post('txtsearch');

        if (empty($who))
            redirect("estudiante/inicio");
        else {
            // echo $who." busqueda";

            $this->data['estudiantes'] = $this->Estudiante_Model->getBusquedaEstudiante($who, $this->input->post('tipo_busqueda'));
            $this->_render('estudiantes/start');
        }
    }

    public
            function ingresodatos() {

        $this->load->library('form_validation');

        $this->form_validation->set_rules('cedula_pers', 'NUMERO DE CEDULA', 'trim|integer|required|is_unique[persona.cedula_pers]');
        $this->form_validation->set_rules('nombre_pers', 'NOMBRES', 'required|trim');
        $this->form_validation->set_rules('apellido_pers', 'APELLIDOS', 'required|trim');
        $this->form_validation->set_rules('nacionalidad_pers', 'NACIONALIDAD', 'required|trim');
        $this->form_validation->set_rules('provincia_pers', 'PROVINCIA', 'required|trim');
        $this->form_validation->set_rules('ciudad_pers', 'CIUDAD', 'required|trim');
        $this->form_validation->set_rules('direccion_pers', 'DIRECCION', 'required|trim');
        $this->form_validation->set_rules('email_pers_est', 'Email personal', 'required|trim|valid_email|is_unique[estudiante.email_pers_est]');
        $this->form_validation->set_rules('planted_procede_est', 'NOMBRE DEL PLANTEL DONDE PROCEDE', 'required|trim');
        $this->form_validation->set_rules('especialidad_est', 'NOMBRE DE LA ESPECIALIDAD', 'required|trim');
        $this->form_validation->set_rules('nombre_madre_est', 'NOMBRE DEL REPRESENTATE', 'required|trim');


        if (($this->form_validation->run() == FALSE)) {
            //echo "algun error";
            $this->_render('estudiantes/new');
        } else {
            $username = substr($this->input->post('nombre_pers'), 0, strpos($this->input->post('nombre_pers'), " "))
                    . substr($this->input->post('apellido_pers'), 0, 1)
                    . substr(md5(microtime()), 1, 3);

            //$v=!empty($this->input->post('foto_pers'));
            //if (isset($_FILES['image']) && !empty($_FILES['image']['name']))

            $error_fatal = 0;

            if (!empty($_FILES['foto_pers']['name'])) {
                $config['upload_path'] = './commons/personas/';
                $config['allowed_types'] = 'gif|jpg|png|jpeg';
                $config['max_size'] = '3000';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';

                $this->load->library('upload', $config);

                if (!$this->upload->do_upload('foto_pers')) {
                    $this->data['msj_error'] = $this->upload->display_errors();
                    $upload_data = null;
                    $error_fatal = 0;
                } else {
                    $upload_data = $this->upload->data();
                    $error_fatal = 1;
                }
            } else {
                $error_fatal = 2;
            }
            switch ($error_fatal) {

                case 0:
                    $this->_render('estudiantes/new');
                    break;
                case 1:
                case 2:
                    $persona = array(
                        "cedula_pers" => $this->input->post('cedula_pers'),
                        "tipo_doc_pers" => $this->input->post('tipo_doc_pers'),
                        "nombre_pers" => $this->input->post('nombre_pers'),
                        "apellido_pers" => $this->input->post('apellido_pers'),
                        "nacionalidad_pers" => $this->input->post('nacionalidad_pers'),
                        "provincia_pers" => $this->input->post('provincia_pers'),
                        "ciudad_pers" => $this->input->post('ciudad_pers'),
                        "direccion_pers" => $this->input->post('direccion_pers'),
                        "refer_domic_pers" => $this->input->post('refer_domic_pers'),
                        "tipo_discap_pers" => $this->input->post('tipo_discap_pers'),
                        "conadis_pers" => $this->input->post('conadis_pers'),
                        "porc_disc_pers" => $this->input->post('porc_disc_pers'),
                        "sexo_pers" => $this->input->post('sexo_pers'),
                        "estado_civil_pers" => $this->input->post('estado_civil_pers'),
                        "autodefin_pers" => $this->input->post('autodefin_pers'),
                        "fecha_nac_pers" => date("Y-m-d", strtotime($this->input->post('fecha_nac_pers'))),
                        "fono_pers" => $this->input->post('fono_pers'),
                        "celular_pers" => $this->input->post('celular_pers'),
                        "email_inst_pers" => $this->input->post('email_inst_pers'),
                        "foto_pers" => ($error_fatal == 1 ? base_url() . "commons/personas/" . $_FILES['foto_pers']['name'] : base_url() . "commons/personas/foto.jpg"),
                        "username_pers" => $username);

                    //$persona_ide_pers=1;
                    $persona_ide_pers = $this->Persona_Model->grabarPersona($persona);
                    $estudiante = array(
                        "persona_ide_pers" => $persona_ide_pers,
                        "planted_procede_est" => $this->input->post('planted_procede_est'),
                        "especialidad_est" => $this->input->post('especialidad_est'),
                        "nombre_padre_est" => $this->input->post('nombre_padre_est'),
                        "profesion_padre_est" => $this->input->post('profesion_padre_est'),
                        "nombre_madre_est" => $this->input->post('nombre_madre_est'),
                        "profesion_madre_est" => $this->input->post('profesion_madre_est'),
                        "conjuge_est" => $this->input->post('conjuge_est'),
                        "carga_fam_est" => $this->input->post('carga_fam_est'),
                        "tipo_sangre_est" => $this->input->post('tipo_sangre_est'),
                        "sector_est" => $this->input->post('sector_est'),
                        "email_pers_est" => $this->input->post('email_pers_est'),
                        "face_est" => $this->input->post('face_est'),
                        "twit_est" => $this->input->post('twit_est'),
                        "estud_traba_est" => $this->input->post('estud_traba_est'),
                        "activo_est" => $this->input->post('activo_est'),
                    );



                    $this->Estudiante_Model->grabarEstudiante($estudiante);
                    redirect('estudiante/inicio');


                    break;
            }
        }
    }

}

/* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */



    